<?php

namespace app\api\controller;
use think\Controller;
use think\Db;

class Register extends  Controller
{
    //注册
    public function register($tel='',$email='',$password='',$code='')
    {
        if(empty($tel)){
            return result(500,'手机号码不能为空！');
        }
        if(! \app\library\Validate::phone($tel))
        {
            return result(500,'手机号码格式有误！');
        }
        if(Db::name('user')->where(['tel'=>$tel])->count()>0){
            return result(500,'该手机号码已注册，请输入其它手机号！');
        }

        $password=trim($password);
        if(empty($password))
        {
            return result(500,'密码不为空！');
        }
        if(mb_strlen($password)<6)
        {
            return result(500,'密码长度不能少于6位字符！');
        }

        if( !empty($email) && ! \app\library\Validate::email($email))
        {
            return result(500,'请输入正确的邮箱地址,或者不填！');
        }
        if(!empty($email) && Db::name('user')->where(['email'=>$email])->count()>0){
            return result(500,'该邮箱已注册，请输入其它邮箱！');
        }

        $captcha = new \think\captcha\Captcha();

        if(empty($code) || ! $captcha->check($code)){
            return result(500,'验证码错误，请重新输入！');
        }

        $data=[
            'tel'=>$tel,
            'email'=>$email,
            'password'=>$password,
        ];

        Db::name('user')->insertGetId($data);

        return result(200,'注册成功！');
    }
}